package pt.ipleiria.plp.ui;

import de.enough.polish.event.ThreadedCommandListener;
import de.enough.polish.midp.ui.Form;
import de.enough.polish.ui.Alert;
import de.enough.polish.ui.AlertType;
import de.enough.polish.ui.Command;
import de.enough.polish.ui.CommandListener;
import de.enough.polish.ui.Displayable;
import de.enough.polish.ui.List;
import de.enough.polish.util.Locale;
//#if polish.api.mmapi
import de.enough.polish.ui.SnapshotScreen;
//#endif
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Image;
import javax.microedition.media.MediaException;
import javax.microedition.media.control.VideoControl;
import pt.ipleiria.plp.ImagePath;
import pt.ipleiria.plp.Utils;

/**
 *
 * @author Ricardo
 */
public class TirarFotoScreen implements CommandListener {

    private Command cmdTakeScreenshot = new Command(Locale.get("foto.newf"), Command.OK, 3);
    private Command cmdQuit = new Command(Locale.get("cmd.Sair"), Command.EXIT, 9);
    private EventoScreen screen;

    public TirarFotoScreen(EventoScreen screen) {
        this.screen = screen;
        //#if polish.api.mmapi
        SnapshotScreen snapshotScreen = new SnapshotScreen("Tirar Fotografia");
        snapshotScreen.addCommand(this.cmdQuit);
        snapshotScreen.addCommand(this.cmdTakeScreenshot);
        snapshotScreen.setCommandListener(new ThreadedCommandListener(this));
        screen.getMidlet().getDisplay().setCurrent(snapshotScreen);
        //#endif
    }

    public void commandAction(Command cmd, Displayable dis) {
        if (cmd == this.cmdQuit) {
            screen.returnFromPhoto(null);
            //#if polish.api.mmapi
        } else if (cmd == this.cmdTakeScreenshot) {
            SnapshotScreen snapshotScreen = (SnapshotScreen) dis;
            try {
                //"encoding=jpeg&width=160&height=120"
                System.out.println("RES: " + snapshotScreen.getDefaultResolution());
                System.out.println("ENCS: " + snapshotScreen.getSnapshotEncodings()[0]);
                System.out.println("ENCS: " + snapshotScreen.getSnapshotEncodings()[1]);
                System.out.println("FORM: " + snapshotScreen.getSnapshotFormats()[0]);
                System.out.println("FORM: " + snapshotScreen.getSnapshotFormats()[1]);


                byte[] image = snapshotScreen.getSnapshot(SnapshotScreen.getSnapshotEncodings()[0]);
                String path = System.getProperty("fileconn.dir.photos");
                String name = saveImage(image, path);
                if (name != null) {
                    ImagePath[] images = new ImagePath[1];
                    images[0] = new ImagePath(path, name, "", "image/jpg");
                    screen.returnFromPhoto(images);
                } else {
                    screen.returnFromPhoto(null);
                }

                //screen.returnFromPhoto(name,path);
            } catch (MediaException e) {
                Alert alert = new Alert("Media-Error", e.toString(), null, AlertType.ERROR);
                screen.getMidlet().getDisplay().setCurrent(alert);
            }
            //#endif
        }
    }

    public String saveImage(byte[] image, String path) {
        String name = null;
        try {

            Date d = new Date();
            name = " " + d.getTime() + ".jpg";
            FileConnection fconn = (FileConnection) Connector.open(path + name);
            if (!fconn.exists()) {
                fconn.create();  // create the file if it doesn't exist
                OutputStream ops = fconn.openOutputStream();
                ops.write(image);
                ops.close();
            }
            fconn.close();
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            return null;
        }
        return name;
    }

    public byte[] get_Byte_Array(Image img) {

        int[] imgRgbData = new int[img.getWidth() * img.getHeight()];
        byte[] imageData = null;
        try {
            img.getRGB(imgRgbData, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());
        } catch (Exception e) {
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);

        try {
            for (int i = 0; i < imgRgbData.length; i++) {
                dos.writeInt(imgRgbData[i]);
            }

            imageData = baos.toByteArray();
            baos.close();
            dos.close();
        } catch (Exception e) {
        }
        return imageData;
    }
}


